{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "provenance": []
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "cells": [
    {
      "cell_type": "markdown",
      "source": [
        "[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/drive/1wfaaIZfBettCzyZqvYn6LeCOgORgq-Td?usp=sharing)"
      ],
      "metadata": {
        "id": "pNpHQn6FlCL1"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "# Testing Vision models with a Single API"
      ],
      "metadata": {
        "id": "ynEbjiyQlJat"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "It is imperative to test different models for your use case to ensure optimal performance and accuracy. However, this process can be cumbersome, as it often involves downloading multiple packages and navigating various APIs. The complexity and effort required can be a significant barrier to effective evaluation.\n",
        "\n",
        "Portkey simplifies this process significantly. As an open-source [**AI Gateway**](https://github.com/Portkey-AI/gateway), Portkey provides a streamlined solution to manage access to over 250 models through a single, unified API. This not only saves time and reduces complexity but also offers valuable insights into cost, performance, and accuracy metrics.\n",
        "\n",
        "With Portkey, you can efficiently compare and evaluate a wide range of LLMs, ensuring you choose the best model for your needs."
      ],
      "metadata": {
        "id": "bUQdnOHYqWLj"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "In this notebook, we illustrate how to run different vision models with a single unified API using Portkey."
      ],
      "metadata": {
        "id": "0emCxX1Algw0"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "### Quickstart"
      ],
      "metadata": {
        "id": "a7sDiU-IGzEm"
      }
    },
    {
      "cell_type": "code",
      "execution_count": 1,
      "metadata": {
        "id": "KldJobxHjBNu",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "62e2cabb-a0f2-4531-b070-94886214db37"
      },
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m92.7/92.7 kB\u001b[0m \u001b[31m1.1 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
            "\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m327.4/327.4 kB\u001b[0m \u001b[31m4.7 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
            "\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m75.6/75.6 kB\u001b[0m \u001b[31m3.5 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
            "\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m12.7/12.7 MB\u001b[0m \u001b[31m14.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
            "\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m77.9/77.9 kB\u001b[0m \u001b[31m4.7 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
            "\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m58.3/58.3 kB\u001b[0m \u001b[31m4.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
            "\u001b[?25h"
          ]
        }
      ],
      "source": [
        "!pip install -qU portkey-ai openai"
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "To start, get your Portkey API key by signing up https://app.portkey.ai/. (Go to \"Settings\" -> \"API Keys\" -> Create an API key with apporpriate scope)"
      ],
      "metadata": {
        "id": "u281LJpvOhjv"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "### Using Portkey to run multiple LLMs"
      ],
      "metadata": {
        "id": "tA9Piq_tHYAt"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "## List of Popular vision models\n",
        "vision_models = [\n",
        "    [\"gpt-4o\", \"openai\"],\n",
        "    [\"gpt-4-turbo\", \"openai\"],\n",
        "    [\"claude-3-opus-20240229\", \"anthropic\"],\n",
        "    [\"claude-3-sonnet-20240229\", \"anthropic\"],\n",
        "    [\"claude-3-haiku-20240307\", \"anthropic\"],\n",
        "]\n",
        "\n",
        "## Replace this with your own virtual keys\n",
        "virtual_keys = {\n",
        "    \"openai\": \"gpt3-8070a6\",                     # Grab from https://platform.openai.com/\n",
        "    \"anthropic\": \"anthropic-9e8db9\",               # Grab from https://console.anthropic.com/\n",
        "}"
      ],
      "metadata": {
        "id": "ZPlY4GC1sBHK"
      },
      "execution_count": 3,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "from google.colab import userdata\n",
        "\n",
        "PORTKEY_API_KEY = userdata.get('PORTKEY_API_KEY')"
      ],
      "metadata": {
        "id": "hLIS_dGdaeWs"
      },
      "execution_count": 2,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "from openai import OpenAI\n",
        "from portkey_ai import PORTKEY_GATEWAY_URL, createHeaders\n",
        "import json\n",
        "\n",
        "# Using the OpenAI SDK to run multiple LLMs in a loop\n",
        "\n",
        "def compare_vision_models(prompt, img_url):\n",
        "    outputs = {}\n",
        "    img_url = img_url\n",
        "\n",
        "\n",
        "    for model, provider in vision_models:\n",
        "        portkey = OpenAI(\n",
        "            api_key = \"dummy_key\",\n",
        "            base_url = PORTKEY_GATEWAY_URL,\n",
        "            default_headers = createHeaders(\n",
        "                api_key=PORTKEY_API_KEY,\n",
        "                virtual_key = virtual_keys[provider]\n",
        "            )\n",
        "        )\n",
        "\n",
        "        response = portkey.chat.completions.create(\n",
        "            messages=[\n",
        "          {\"role\": \"user\", \"content\": [\n",
        "              {\"type\": \"text\", \"text\": prompt},\n",
        "              {\"type\": \"image_url\", \"image_url\": {\n",
        "                  \"url\": img_url}\n",
        "              }\n",
        "          ]}\n",
        "          ],\n",
        "            model=model,\n",
        "            max_tokens=256\n",
        "        )\n",
        "\n",
        "        content = response.choices[0]\n",
        "        outputs[model] =content.message.content\n",
        "\n",
        "    return outputs"
      ],
      "metadata": {
        "id": "21C7SyO4dzwI"
      },
      "execution_count": 8,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "from tabulate import tabulate\n",
        "\n",
        "def print_model_outputs(prompt, img_url):\n",
        "    outputs = compare_vision_models(prompt, img_url)\n",
        "\n",
        "    table_data = []\n",
        "    for model, output in outputs.items():\n",
        "        table_data.append([model, output.strip()])\n",
        "\n",
        "    headers = [\"Model\", \"Output\"]\n",
        "    table = tabulate(table_data, headers, tablefmt=\"grid\")\n",
        "    print(table)\n",
        "    print()"
      ],
      "metadata": {
        "id": "Z0y5BPgRvwIC"
      },
      "execution_count": 6,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "print_model_outputs(prompt = \"Solve the question\",\n",
        "                    img_url = \"\"\n",
        "                    )"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "Feup1e8obFA-",
        "outputId": "ca3442a7-186d-41f6-8322-4ab0ab411d16"
      },
      "execution_count": 16,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "+--------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+\n",
            "| Model                    | Output                                                                                                                                                                                  |\n",
            "+==========================+=========================================================================================================================================================================================+\n",
            "| gpt-4o                   | To find the unknown angle in a triangle, we can use the fact that the sum of the internal angles of a triangle is always 180°.                                                          |\n",
            "|                          |                                                                                                                                                                                         |\n",
            "|                          | Given:                                                                                                                                                                                  |\n",
            "|                          | Angle 1 = 95°                                                                                                                                                                           |\n",
            "|                          | Angle 2 = 35°                                                                                                                                                                           |\n",
            "|                          |                                                                                                                                                                                         |\n",
            "|                          | Let the unknown angle be \\( \\theta \\).                                                                                                                                                  |\n",
            "|                          |                                                                                                                                                                                         |\n",
            "|                          | \\[ 95° + 35° + \\theta = 180° \\]                                                                                                                                                         |\n",
            "|                          |                                                                                                                                                                                         |\n",
            "|                          | \\[ 130° + \\theta = 180° \\]                                                                                                                                                              |\n",
            "|                          |                                                                                                                                                                                         |\n",
            "|                          | Subtracting 130° from both sides gives:                                                                                                                                                 |\n",
            "|                          |                                                                                                                                                                                         |\n",
            "|                          | \\[ \\theta = 180° - 130° \\]                                                                                                                                                              |\n",
            "|                          |                                                                                                                                                                                         |\n",
            "|                          | \\[ \\theta = 50° \\]                                                                                                                                                                      |\n",
            "|                          |                                                                                                                                                                                         |\n",
            "|                          | Therefore, the unknown angle is \\( 50° \\).                                                                                                                                              |\n",
            "+--------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+\n",
            "| gpt-4-turbo              | To solve for the unknown angle in the triangle, we can use the fact that the sum of all interior angles in a triangle is always 180 degrees.                                            |\n",
            "|                          |                                                                                                                                                                                         |\n",
            "|                          | The given triangle has two angles already noted: 95 degrees and 35 degrees. Let's denote the unknown angle as \\( \\theta \\).                                                             |\n",
            "|                          |                                                                                                                                                                                         |\n",
            "|                          | We can set up the equation:                                                                                                                                                             |\n",
            "|                          | \\[ 95^\\circ + 35^\\circ + \\theta = 180^\\circ \\]                                                                                                                                          |\n",
            "|                          |                                                                                                                                                                                         |\n",
            "|                          | Adding 95 degrees and 35 degrees gives:                                                                                                                                                 |\n",
            "|                          | \\[ 130^\\circ + \\theta = 180^\\circ \\]                                                                                                                                                    |\n",
            "|                          |                                                                                                                                                                                         |\n",
            "|                          | Now, solving for \\( \\theta \\):                                                                                                                                                          |\n",
            "|                          | \\[ \\theta = 180^\\circ - 130^\\circ \\]                                                                                                                                                    |\n",
            "|                          | \\[ \\theta = 50^\\circ \\]                                                                                                                                                                 |\n",
            "|                          |                                                                                                                                                                                         |\n",
            "|                          | Therefore, the unknown angle in the triangle is \\( 50^\\circ \\).                                                                                                                         |\n",
            "+--------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+\n",
            "| claude-3-opus-20240229   | Let's analyze the given problem step by step to find the measure of angle x.                                                                                                            |\n",
            "|                          |                                                                                                                                                                                         |\n",
            "|                          | 1. We are given a triangle with two labeled angles - 95° and 35°.                                                                                                                       |\n",
            "|                          | 2. In a triangle, the sum of all interior angles is always 180°.                                                                                                                        |\n",
            "|                          | 3. So, if we know two angles, we can find the third angle by subtracting the sum of the known angles from 180°.                                                                         |\n",
            "|                          |                                                                                                                                                                                         |\n",
            "|                          | In this case:                                                                                                                                                                           |\n",
            "|                          | - Angle 1 = 95°                                                                                                                                                                         |\n",
            "|                          | - Angle 2 = 35°                                                                                                                                                                         |\n",
            "|                          | - Sum of known angles = 95° + 35° = 130°                                                                                                                                                |\n",
            "|                          |                                                                                                                                                                                         |\n",
            "|                          | Now, let's find angle x:                                                                                                                                                                |\n",
            "|                          | - Angle x = 180° - 130° = 50°                                                                                                                                                           |\n",
            "|                          |                                                                                                                                                                                         |\n",
            "|                          | Therefore, the measure of angle x is 50°.                                                                                                                                               |\n",
            "+--------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+\n",
            "| claude-3-sonnet-20240229 | Based on the given image, this is a question about finding an angle in a triangle.                                                                                                      |\n",
            "|                          |                                                                                                                                                                                         |\n",
            "|                          | The image shows a triangle with one angle marked as 85 degrees and another angle marked as 35 degrees.                                                                                  |\n",
            "|                          |                                                                                                                                                                                         |\n",
            "|                          | Since the sum of the angles in any triangle is 180 degrees, we can find the third and remaining angle by subtracting the given angles (85 + 35) from 180.                               |\n",
            "|                          |                                                                                                                                                                                         |\n",
            "|                          | 180 degrees - (85 degrees + 35 degrees) = 180 degrees - 120 degrees = 60 degrees                                                                                                        |\n",
            "|                          |                                                                                                                                                                                         |\n",
            "|                          | Therefore, the third and unmarked angle in the triangle is 60 degrees.                                                                                                                  |\n",
            "+--------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+\n",
            "| claude-3-haiku-20240307  | The image shows a triangle with two labeled angles - 95° and 35°. To solve for the missing angle, we can use the fact that the sum of the interior angles of a triangle is always 180°. |\n",
            "|                          |                                                                                                                                                                                         |\n",
            "|                          | To find the third angle, we can use the formula:                                                                                                                                        |\n",
            "|                          |                                                                                                                                                                                         |\n",
            "|                          | Sum of the interior angles of a triangle = 180°                                                                                                                                         |\n",
            "|                          |                                                                                                                                                                                         |\n",
            "|                          | Plugging in the given angles:                                                                                                                                                           |\n",
            "|                          | 95° + 35° + x = 180°                                                                                                                                                                    |\n",
            "|                          | Where x is the unknown third angle.                                                                                                                                                     |\n",
            "|                          |                                                                                                                                                                                         |\n",
            "|                          | Solving for x:                                                                                                                                                                          |\n",
            "|                          | 130° + x = 180°                                                                                                                                                                         |\n",
            "|                          | x = 180° - 130°                                                                                                                                                                         |\n",
            "|                          | x = 50°                                                                                                                                                                                 |\n",
            "|                          |                                                                                                                                                                                         |\n",
            "|                          | Therefore, the missing angle in the triangle is 50°.                                                                                                                                    |\n",
            "+--------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+\n",
            "\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [],
      "metadata": {
        "id": "tEdJQxaqbQC6"
      },
      "execution_count": null,
      "outputs": []
    }
  ]
}